package Java学习;

import java.util.Random;

public class 插入排序 {
    public static int k;
    public static void main(String[] args) {
        Random rm = new Random();
        int[] arr = new int[100000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = rm.nextInt();
        }

        long start = System.currentTimeMillis();
        for (int i = 0; i < arr.length-1; i++) {
            if (arr[i]>arr[i+1]){
                k = i+1;
                break;
            }
        }
        for (int i = k; i < arr.length; i++) {
            int j = i;
            while (j > 0 && arr[j]<arr[j-1]){
                int stem = arr[j-1];
                arr[j-1] = arr[j];
                arr[j] = stem;
                j--;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
//        for (int i = 0; i < arr.length; i++) {
//            System.out.print(arr[i] + " ");
//        }
    }
}
